"""
python爬取小说
"""
# 怎么发送请求
# pip install requests
import requests
# pip install lxml
from lxml import etree

# 目标地址
url = 'https://www.85xs.cc/book/douluodalu1/1.html'
# 伪装自己
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
}
while True:
    try:
        # 发送请求
        resp = requests.get(url, headers=headers)

        # 设置编码
        resp.encoding = 'utf-8'
        # 响应信息
        # print(resp.text)

        e = etree.HTML(resp.text)
        info = '\n'.join(e.xpath('//div[@class ="m-post"]/p/text()'))
        title = e.xpath('//h1/text()')[0]
        url = f'https://www.85xs.cc{e.xpath("//tr/td[2]/a/@href")[0]}'

        # print(info)
        print(title)
        # 保存
        with open('data/story.txt', 'w', encoding='utf-8') as f:
            f.write(title + '\n\n' + info + '\n\n')

        if url == '/book/douluodalu1/':
            break
    except:
        break